package com.sidianzhong.sdz.controller.common;

import com.alibaba.fastjson.JSONObject;
import com.sidianzhong.sdz.annotation.BackLoginToken;
import com.sidianzhong.sdz.model.*;
import com.sidianzhong.sdz.model.commond.UserToken;
import com.sidianzhong.sdz.service.*;
import com.sidianzhong.sdz.service.commond.RedisService;
import com.sidianzhong.sdz.utils.International;
import com.sidianzhong.sdz.utils.Matches;
import com.sidianzhong.sdz.utils.PageInfo;
import com.sidianzhong.sdz.utils.ResultModel;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.ResourceBundle;
import java.util.stream.Collectors;

@Api(description = "后台接口")
@Controller
public class BackController {

    @Autowired
    AdminService adminService;
    @Autowired
    RedisService redisService;
    @Autowired
    UserService userService;
    @Autowired
    UserWalletService userWalletService;
    @Autowired
    ConstantService constantService;
    @Autowired
    CoinInfoService coinInfoService;

    @ApiOperation(value = "后台登录")
    @RequestMapping(value = "/backLogin",
            method = RequestMethod.POST,
            produces = "application/json;charset=UTF-8")
    @ResponseBody
    public Object BackLogin(
            HttpServletRequest request,
            @RequestParam(value = "phone") String phone,
            @RequestParam(value = "password") String password,
            @RequestParam(value = "area", required = false, defaultValue = "86") String area
    ) {

        ResourceBundle myResourcesBundle = International.getLanguage(request);
        if (phone == null || phone.isEmpty() || !phone.matches(Matches.PHONE_MATCHES))
            return new ResponseEntity<>(ResultModel.error(myResourcesBundle, "userNameOrPassWordError"), HttpStatus.OK);
        if (password == null || password.isEmpty() || !password.matches(Matches.PASSWORD_MATCHES))
            return new ResponseEntity<>(ResultModel.error(myResourcesBundle, "userNameOrPassWordError"), HttpStatus.OK);

        Admin admin = adminService.getByPhone(phone);
        if (admin == null) {
            return new ResponseEntity<>(ResultModel.error(myResourcesBundle, "userNotFound"), HttpStatus.OK);
        }
        //验证密码是否正确
        if (!admin.getPassword().equals(password)) {
            return new ResponseEntity<>(ResultModel.error(myResourcesBundle, "userNameOrPassWordError"), HttpStatus.OK);
        }
        //登陆成功
        User user = new User();
        user.setId(admin.getId());
        UserToken userToken = redisService.createToken(user);
        return new ResponseEntity<>(ResultModel.ok(userToken), HttpStatus.OK);
    }

    @BackLoginToken
    @ApiOperation(value = "用户列表")
    @RequestMapping(value = "/userList",
            method = RequestMethod.POST,
            produces = "application/json;charset=UTF-8")
    @ResponseBody
    public Object userList(
            HttpServletRequest request,
            @RequestHeader(value = "X-Auth-Token") String token,
            @RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize,
            @RequestParam(value = "sortItem", required = false, defaultValue = "id") String sortItem,
            @RequestParam(value = "sortOrder", required = false, defaultValue = "asc") String sortOrder,
            @RequestParam(value = "phone", required = false) String phone
    ) {

        User user  = new User();
        boolean empty = phone.isEmpty();
        boolean equals = !phone.equals("");
        if (phone!=null && !phone.isEmpty() && !phone.equals("")){
            user.setPhone(phone);
        }
        List<User> list = userService.getList(pageNum, pageSize, sortItem, sortOrder, user);
        List<JSONObject> collect = list.stream().map(user1 -> {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("id", user1.getId());
            jsonObject.put("userName", user1.getUserName());
            jsonObject.put("phone", user1.getPhone());
            jsonObject.put("area", user1.getArea());
            jsonObject.put("status", user1.getStatus());
            return jsonObject;
        }).collect(Collectors.toList());
        PageInfo result = new PageInfo(collect);
        return new ResponseEntity<>(ResultModel.ok(result), HttpStatus.OK);
    }

    @BackLoginToken
    @ApiOperation(value = "用户钱包")
    @RequestMapping(value = "/userWalletList",
            method = RequestMethod.POST,
            produces = "application/json;charset=UTF-8")
    @ResponseBody
    public Object userWalletList(
            HttpServletRequest request,
            @RequestHeader(value = "X-Auth-Token") String token,
            @RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize,
            @RequestParam(value = "sortItem", required = false, defaultValue = "id") String sortItem,
            @RequestParam(value = "sortOrder", required = false, defaultValue = "asc") String sortOrder,
            @RequestParam(value = "userId") Integer userId
    ) {
        UserWallet userWallet = new UserWallet();
        userWallet.setUserId(userId);
        List<UserWallet> list = userWalletService.getList(pageNum, pageSize, sortItem, sortOrder, userWallet);
        List<JSONObject> collect = list.stream().map(ls -> {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("address", ls.getAddress());
            jsonObject.put("userId", ls.getUserId());
            jsonObject.put("amount", ls.getAmount());
            jsonObject.put("status", ls.getStatus());
            CoinInfo coinInfo = coinInfoService.get(ls.getCoinId());
            jsonObject.put("coinId", ls.getCoinId());
            jsonObject.put("coinName", coinInfo.getCoinSymbol());
            return jsonObject;
        }).collect(Collectors.toList());
        return new ResponseEntity<>(ResultModel.ok(collect), HttpStatus.OK);
    }

    @BackLoginToken
    @ApiOperation(value = "常量")
    @RequestMapping(value = "/noahConstant",
            method = RequestMethod.POST,
            produces = "application/json;charset=UTF-8")
    @ResponseBody
    public Object noahConstant(
            HttpServletRequest request,
            @RequestHeader(value = "X-Auth-Token") String token,
            @RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize,
            @RequestParam(value = "sortItem", required = false, defaultValue = "id") String sortItem,
            @RequestParam(value = "sortOrder", required = false, defaultValue = "desc") String sortOrder

    ) {
        Constant item = new Constant();
        PageInfo<Constant> list =  constantService.getListWithPaging(pageNum, pageSize, sortItem, sortOrder,item);
        return new ResponseEntity<>(ResultModel.ok(list), HttpStatus.OK);
    }







}
